package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.dto.ArticleStreamMessage;
import com.heima.article.service.IComputeService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class ArticleResultListener {

    @Autowired
    IComputeService computeService; // 计算热点文章分值

    @KafkaListener(topics = "article_result") // 监听的主题
    public void handleMsg(ConsumerRecord<String, String> record){
        //1. 接收到的聚合结果消息
        String value = record.value();
        System.out.println("接收到的聚合结果消息: " + value);
        //2. 转换成对象
        ArticleStreamMessage message = JSON.parseObject(value, ArticleStreamMessage.class);
        //3. 缓存和数据库的更新
        computeService.update(message);
    }
}
